package com.aggressionlabs.test;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

/**
 * Created by shirland on 12/20/14.
 */
public class RunningConfig {

    private int serverPort;
    private String realmName;
    private String logDirectory;

    private ArrayList<HandlerConfig> theHandlers = null;

    public ArrayList<HandlerConfig> getHandlerConfigs() {
        return theHandlers;
    }

    public int getServerPort(){
        return serverPort;
    }

    public String getLogDirectory(){
    	return logDirectory;
    }

    public RunningConfig(String fileName) throws XMLStreamException {

        theHandlers = new ArrayList<HandlerConfig>();

        File f = new File(fileName);
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        XMLInputFactory xmlFactory = XMLInputFactory.newFactory();
        XMLStreamReader reader = xmlFactory.createXMLStreamReader(fis);

        //maintain structure
        boolean inHandlers = false;
        boolean inAHandler = false;


        //handler variables
        String tmpHandlerClass = null;
        String tmpHandlerContext = null;
        String tmpHandlerRole = null;

        while (reader.hasNext()) {

            int event = reader.next();

            switch (event) {
                case XMLStreamConstants.START_ELEMENT:

                    if (reader.getLocalName().equals("server-port")) {
                        serverPort = Integer.parseInt(reader.getElementText());
                    }

                    if (reader.getLocalName().equals("log-directory")) {
                        logDirectory = reader.getElementText();
                    }

                    if (reader.getLocalName().equals("realm-name")) {
                        realmName = reader.getElementText();
                    }

                    if (reader.getLocalName().equals("handlers")) {
                        inHandlers = true;
                    }

                    if (reader.getLocalName().equals("handler") && inHandlers) {
                        inAHandler = true;
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            if (reader.getAttributeLocalName(i).equals("class"))
                                tmpHandlerClass = reader.getAttributeValue(i);
                            else if (reader.getAttributeLocalName(i).equals("context"))
                                tmpHandlerContext = reader.getAttributeValue(i);
                        }
                    }

                    if (reader.getLocalName().equals("security") && inAHandler)
                        tmpHandlerRole = reader.getElementText();
                    break;


                case XMLStreamConstants.END_ELEMENT:
                    if (reader.getLocalName().equals("handler")) {
                        HandlerConfig handler = new HandlerConfig(tmpHandlerClass, tmpHandlerContext, tmpHandlerRole);
                        theHandlers.add(handler);
                        inAHandler = false;
                    }

                    if (reader.getLocalName().equals("handlers")) {
                        inHandlers = false;
                    }

                    break;
            }
        }

    }

    public String getRealmName() {
        return realmName;
    }

    public class HandlerConfig {

        private String theClass = null;
        private String context = null;
        private String roleAccess = null;

        private HandlerConfig(String inClass, String inContext, String inRole) {

            theClass = inClass;
            context = inContext;
            roleAccess = inRole;
        }

        public String getClassName() {
            return theClass;
        }

        public String getContext() {
            return context;
        }

        public String getRoleAccess() {
            return roleAccess;
        }

    }
}
